package com.smartleanx.module.hrm.service.salary;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.smartleanx.framework.common.pojo.CommonResult;
import com.smartleanx.module.hrm.dal.dataobject.employee.EmployeeDO;
import com.smartleanx.module.hrm.dal.dataobject.employee.EmployeeSeparationRecordDO;
import com.smartleanx.module.hrm.dal.dataobject.salary.*;
import com.smartleanx.module.hrm.dal.dataobject.salary.vo.FixedEmployeeUpdateVo;
import com.smartleanx.module.hrm.dal.dataobject.salary.vo.FixedEmployeeVo;
import com.smartleanx.module.hrm.dal.mysql.employee.EmployeeMapper;
import com.smartleanx.module.hrm.dal.mysql.employee.EmployeeSeparationRecordMapper;
import com.smartleanx.module.hrm.dal.mysql.salary.*;
import com.smartleanx.module.hrm.service.examine.util.DateUtil;
import com.smartleanx.module.hrm.service.examine.util.exception.ResultException;
import com.smartleanx.module.system.api.dept.DeptApi;
import com.smartleanx.module.system.api.dept.PostApi;
import com.smartleanx.module.system.api.dept.PostLevelApi;
import com.smartleanx.module.system.api.dept.dto.DeptRespDTO;
import com.smartleanx.module.system.api.dept.dto.PostLevelRespDTO;
import com.smartleanx.module.system.api.dept.dto.PostRespDTO;
import com.smartleanx.module.system.api.dict.DictDataApi;
import com.smartleanx.module.system.api.dict.dto.DictDataRespDTO;
import org.springdoc.core.GenericParameterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author mybatis-plus
 * @since 2025-01-05
 */
@Service
public class HrmSalaryFixedServiceImpl implements HrmSalaryFixedService {

    @Autowired
    private HrmSalaryFixedMapper hrmSalaryFixedMapper;
    @Autowired
    private DictDataApi dictDataApi;
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private PostApi postApi;
    @Autowired
    private HrmSalaryFixedTableMapper salaryFixedTableMapper;
    @Autowired
    private HrmSalaryTemplateInfoMapper hrmSalaryTemplateInfoMapper;

    @Autowired
    private EmployeeSeparationRecordMapper employeeSeparationRecordMapper;
    @Autowired
    private GenericParameterService parameterBuilder;
    @Autowired
    private HrmSalaryFixedTableMapper hrmSalaryFixedTableMapper;
    @Autowired
    private DeptApi deptApi;
    @Autowired
    private HrmSalaryTableConfigTenantMapper salaryTableConfigTenantMapper;
    @Autowired
    private PostLevelApi postLevelApi;
    private HrmSalaryTemplateMapper hrmSalaryTemplateMapper;

    @Override
    public PageInfo<FixedEmployeeVo> selectFixedList(Integer pageNum, Integer pageSize, Integer postName, Integer postLevelId, Integer employeeFrom, Integer employeeState, Integer employeeResignation, String employeeName, String deptName, String employeeNo,List<Integer> employeeIds) {
        List<FixedEmployeeVo> fixedEmployeeVos = new ArrayList<>();
        Long total = employeeMapper.selectCount(new LambdaQueryWrapper<EmployeeDO>() {{
            eq(!ObjectUtils.isEmpty(postName), EmployeeDO::getPostId, postName);
            eq(!ObjectUtils.isEmpty(postLevelId), EmployeeDO::getPostLevelId, postLevelId);
            eq(!ObjectUtils.isEmpty(employeeFrom), EmployeeDO::getEmploymentForm, employeeFrom);
            eq(!ObjectUtils.isEmpty(employeeState), EmployeeDO::getState, employeeState);
            eq(!ObjectUtils.isEmpty(employeeResignation) && employeeResignation == 2, EmployeeDO::getState, 6);
            ne(!ObjectUtils.isEmpty(employeeResignation) && employeeResignation == 1, EmployeeDO::getState, 6);
            like(!ObjectUtils.isEmpty(employeeName), EmployeeDO::getName, employeeName);
            eq(!ObjectUtils.isEmpty(employeeNo), EmployeeDO::getEmployeeNo, employeeNo);
            in(!ObjectUtils.isEmpty(deptName), EmployeeDO::getDeptId, deptName);
            in(!ObjectUtils.isEmpty(employeeIds),EmployeeDO::getUserId, employeeIds);
        }});
        PageHelper.startPage(pageNum, pageSize);
        List<EmployeeDO> employeeDOS = employeeMapper.selectList(new LambdaQueryWrapper<EmployeeDO>() {{
            eq(!ObjectUtils.isEmpty(postName), EmployeeDO::getPostId, postName);
            eq(!ObjectUtils.isEmpty(postLevelId), EmployeeDO::getPostLevelId, postLevelId);
            eq(!ObjectUtils.isEmpty(employeeFrom), EmployeeDO::getEmploymentForm, employeeFrom);
            eq(!ObjectUtils.isEmpty(employeeState), EmployeeDO::getState, employeeState);
            eq(!ObjectUtils.isEmpty(employeeResignation) && employeeResignation == 2, EmployeeDO::getState, 6);
            ne(!ObjectUtils.isEmpty(employeeResignation) && employeeResignation == 1, EmployeeDO::getState, 6);
            like(!ObjectUtils.isEmpty(employeeName), EmployeeDO::getName, employeeName);
            eq(!ObjectUtils.isEmpty(employeeNo), EmployeeDO::getEmployeeNo, employeeNo);
            in(!ObjectUtils.isEmpty(deptName), EmployeeDO::getDeptId, deptName);
            in(!ObjectUtils.isEmpty(employeeIds),EmployeeDO::getUserId, employeeIds);
            orderByDesc(EmployeeDO::getCreateTime);
        }});
        employeeDOS.forEach(employeeDO -> {
            CommonResult<DeptRespDTO> dept = deptApi.getDept(employeeDO.getDeptId());
            HrmSalaryFixed hrmSalaryFixed = hrmSalaryFixedMapper.selectOne(new LambdaQueryWrapper<HrmSalaryFixed>() {{
                eq(HrmSalaryFixed::getEmployeeId, employeeDO.getId());
                orderByDesc(HrmSalaryFixed::getCreateTime);
                last("limit 1");
            }});
            String employeePost = "--";
            CommonResult<PostRespDTO> post = postApi.getPost(employeeDO.getPostId());
            if (!ObjectUtils.isEmpty(post.getData())) {
                employeePost = post.getCheckedData().getName();
            }
            CommonResult<PostLevelRespDTO> postLevelById = postLevelApi.getPostLevelById(employeeDO.getPostLevelId());
            CommonResult<List<DictDataRespDTO>> hrmEmployeeStatus = dictDataApi.getDictDataList("hrm_employee_status");
            CommonResult<List<DictDataRespDTO>> hrmEmploymentForm = dictDataApi.getDictDataList("hrm_employment_form");
            List<DictDataRespDTO> dictDataRespDTOS = hrmEmployeeStatus.getData();
            List<String> collect = dictDataRespDTOS.stream().filter(d -> d.getValue().equals(employeeDO.getState() + "")).map(di -> di.getLabel()).collect(Collectors.toList());
            List<DictDataRespDTO> dictForm = hrmEmploymentForm.getCheckedData();
            List<String> collect1 = dictForm.stream().filter(d -> d.getValue().equals(employeeDO.getEmploymentForm() + "")).map(di -> di.getLabel()).collect(Collectors.toList());
            FixedEmployeeVo fixedEmployeeVo = new FixedEmployeeVo();
            fixedEmployeeVo.setEmployeeName(employeeDO.getName());
            fixedEmployeeVo.setSex(employeeDO.getSex());
            EmployeeSeparationRecordDO employeeSeparationRecordDO = employeeSeparationRecordMapper.selectOne(new LambdaQueryWrapper<EmployeeSeparationRecordDO>() {{
                eq(EmployeeSeparationRecordDO::getEmployeeId, employeeDO.getId());
                eq(EmployeeSeparationRecordDO::getDeleted, 0);
                eq(EmployeeSeparationRecordDO::getApproveResult, 2);
            }});
            if (!ObjectUtils.isEmpty(dept.getData())) {
                fixedEmployeeVo.setDepartmentName(dept.getData().getName());
            }
            if (employeeDO.getState() == 3 || (!ObjectUtils.isEmpty(employeeSeparationRecordDO) && employeeSeparationRecordDO.getOldStatus() == 3)) {
                fixedEmployeeVo.setEmployeeState(0);
            } else {
                fixedEmployeeVo.setEmployeeState(1);
            }
            if (employeeDO.getState() == 6) {
                fixedEmployeeVo.setEmployeeResignation("离职");
            } else {
                fixedEmployeeVo.setEmployeeResignation("在职");
            }
            if (!ObjectUtils.isEmpty(collect)) {
                fixedEmployeeVo.setEmployeeStatus(collect.get(0));

            } else {
                fixedEmployeeVo.setEmployeeStatus("--");
            }
            if (!ObjectUtils.isEmpty(collect1)) {
                fixedEmployeeVo.setEmployeeFrom(collect1.get(0));
            } else {
                fixedEmployeeVo.setEmployeeFrom("--");
            }
            fixedEmployeeVo.setEmployeeId(employeeDO.getId().intValue());
            fixedEmployeeVo.setEmployeeNo(employeeDO.getEmployeeNo());
            if (!ObjectUtils.isEmpty(employeeDO.getEntryTime())) {
                fixedEmployeeVo.setEntryTime(employeeDO.getEntryTime().toString());
            } else {
//                fixedEmployeeVo.setEntryTime("--");
            }
            if (!ObjectUtils.isEmpty(postLevelById.getData())) {
                fixedEmployeeVo.setPostLevel(postLevelById.getData().getName());
            }
            if (!ObjectUtils.isEmpty(employeeDO.getRegularizationTime())) {
                fixedEmployeeVo.setRegularizationTime(employeeDO.getRegularizationTime().toString());
            } else {
//                fixedEmployeeVo.setRegularizationTime("--");
            }
            fixedEmployeeVo.setPostName(employeePost);
            if (!ObjectUtils.isEmpty(hrmSalaryFixed)) {
                fixedEmployeeVo.setDate(hrmSalaryFixed.getDate());
                if (fixedEmployeeVo.getEmployeeState() == 1) {
                    fixedEmployeeVo.setSumSalary(hrmSalaryFixed.getSySumSalary());
                } else {
                    fixedEmployeeVo.setSumSalary(hrmSalaryFixed.getSumSalary());
                }

                fixedEmployeeVo.setCreateTime(hrmSalaryFixed.getCreateTime());
                fixedEmployeeVo.setId(hrmSalaryFixed.getId());

                switch (hrmSalaryFixed.getReason()) {
                    case 1:
                        fixedEmployeeVo.setReason("入职定薪");
                        break;
                    case 2:
                        fixedEmployeeVo.setReason("入职核定");
                        break;
                    case 3:
                        fixedEmployeeVo.setReason("转正");
                        break;
                    case 4:
                        fixedEmployeeVo.setReason("晋升");
                        break;
                    case 5:
                        fixedEmployeeVo.setReason("调动");
                        break;
                    case 6:
                        fixedEmployeeVo.setReason("年中调薪");
                        break;
                    case 7:
                        fixedEmployeeVo.setReason("年度调薪");
                        break;
                    case 8:
                        fixedEmployeeVo.setReason("特别调薪");
                        break;
                    case 9:
                        fixedEmployeeVo.setReason("其他");
                        break;
                }
                fixedEmployeeVo.setState(1);
            } else {
                fixedEmployeeVo.setState(0);
            }
            fixedEmployeeVos.add(fixedEmployeeVo);
        });
        PageInfo pageInfo = new PageInfo<>(fixedEmployeeVos);
        pageInfo.setTotal(total);
//        pageInfo.setPageSize(total);
        return pageInfo;
    }

    @Override
    public void updateEmployeeFixed(HrmSalaryFixed hrmSalaryFixed) {
        Integer employeeStatus = -1;
        if (ObjectUtils.isEmpty(hrmSalaryFixed.getTableTemplateId())) {
            throw new ResultException("定薪模版不能为空");
        }
        if (ObjectUtils.isEmpty(hrmSalaryFixed.getTableList())) {
            throw new ResultException("定薪项不能为空");
        }
        EmployeeDO employeeDO = employeeMapper.selectById(hrmSalaryFixed.getEmployeeId());
        EmployeeSeparationRecordDO employeeSeparationRecordDO = employeeSeparationRecordMapper.selectOne(new LambdaQueryWrapper<EmployeeSeparationRecordDO>() {{
            eq(EmployeeSeparationRecordDO::getEmployeeId, employeeDO.getId());
            eq(EmployeeSeparationRecordDO::getDeleted, 0);
            eq(EmployeeSeparationRecordDO::getApproveResult, 2);
        }});
        if (employeeDO.getState() == 3 || (!ObjectUtils.isEmpty(employeeSeparationRecordDO) && employeeSeparationRecordDO.getOldStatus() == 3)) {
            employeeStatus = 1;
        } else {
            employeeStatus = 0;
        }
        Long l = hrmSalaryFixedMapper.selectCount(new LambdaQueryWrapper<HrmSalaryFixed>() {{
            eq(HrmSalaryFixed::getEmployeeId, hrmSalaryFixed.getEmployeeId());
            ge(HrmSalaryFixed::getDate, DateUtil.currentDateFormat());
        }});
        if (l > 0) {
            throw new ResultException("员工当前存在正在进行中的调薪任务，无法再次新增调薪任务!");
        }
        List<HrmSalaryTemplateInfo> hrmSalaryTemplateInfos = hrmSalaryTemplateInfoMapper.selectList(new LambdaQueryWrapper<HrmSalaryTemplateInfo>() {{
            eq(HrmSalaryTemplateInfo::getTemplateId, hrmSalaryFixed.getTableTemplateId());
        }});
        if (hrmSalaryFixed.getId() != null) {
            salaryFixedTableMapper.delete(new LambdaQueryWrapper<HrmSalaryFixedTable>() {{
                eq(HrmSalaryFixedTable::getFixedId, hrmSalaryFixed.getId());
            }});
        }
        HrmSalaryFixed hrmSalaryFixed1 = hrmSalaryFixedMapper.selectOne(new LambdaQueryWrapper<HrmSalaryFixed>() {{
            eq(HrmSalaryFixed::getEmployeeId, hrmSalaryFixed.getEmployeeId());
            orderByDesc(HrmSalaryFixed::getDate);
            last("limit 1");
        }});
        if (!ObjectUtils.isEmpty(hrmSalaryFixed1)) {
            hrmSalaryFixed.setOldSumSalary(hrmSalaryFixed1.getSumSalary());
        }
        Long l1 = hrmSalaryFixedMapper.selectCount(null);
        if (l1 == 0) {
            hrmSalaryFixed.setState(0);
        } else {
            hrmSalaryFixed.setState(1);
        }
        hrmSalaryFixed.setIsEffect(1);
        hrmSalaryFixed.setCreateTime(DateUtil.currentDateFormat());
        hrmSalaryFixedMapper.insert(hrmSalaryFixed);
        Integer finalEmployeeStatus = employeeStatus;
        hrmSalaryTemplateInfos.forEach(hrmSalaryTemplateInfo -> {
            List<JSONObject> tableList = hrmSalaryFixed.getTableList();
            List<JSONObject> syTableList = hrmSalaryFixed.getSyTableList();
            Set<JSONObject> parameterBuilder = tableList.stream().filter(a -> a.getInteger("id").intValue() == hrmSalaryTemplateInfo.getTableId()).collect(Collectors.toSet());
            Set<JSONObject> syParameterBuilder = syTableList.stream().filter(a -> a.getInteger("id").intValue() == hrmSalaryTemplateInfo.getTableId()).collect(Collectors.toSet());
            if (!ObjectUtils.isEmpty(parameterBuilder)) {
                if (finalEmployeeStatus == 0) {
                    if (ObjectUtils.isEmpty(hrmSalaryFixed.getSyTableList()) || ObjectUtils.isEmpty(new ArrayList<>(syParameterBuilder).get(0).getBigDecimal("becomeFrontAmount"))) {
                        throw new ResultException("试用期内，试用期薪资不可以为空");
                    }
                } else {
                    if (ObjectUtils.isEmpty(hrmSalaryFixed.getTableList()) || ObjectUtils.isEmpty(new ArrayList<>(parameterBuilder).get(0).getBigDecimal("becomeAfterAmount"))) {
                        throw new ResultException("转正后，正式薪资不可以为空");
                    }
                }
                HrmSalaryFixedTable hrmSalaryFixedTable = new HrmSalaryFixedTable();
                if (finalEmployeeStatus == 0) {
                    hrmSalaryFixedTable.setBecomeFrontAmount(new ArrayList<>(syParameterBuilder).get(0).getBigDecimal("becomeFrontAmount"));
                }
                hrmSalaryFixedTable.setBecomeAfterAmount(new ArrayList<>(parameterBuilder).get(0).getBigDecimal("becomeAfterAmount"));
                hrmSalaryFixedTable.setTableId(hrmSalaryTemplateInfo.getTableId());
                hrmSalaryFixedTable.setFixedId(hrmSalaryFixed.getId());
                hrmSalaryFixedTable.setCreateTime(DateUtil.currentDateFormat());
                if (!ObjectUtils.isEmpty(hrmSalaryFixed1)) {
                    HrmSalaryFixedTable hrmSalaryFixedTable1 = salaryFixedTableMapper.selectOne(new LambdaQueryWrapper<HrmSalaryFixedTable>() {{
                        eq(HrmSalaryFixedTable::getFixedId, hrmSalaryFixed1.getId());
                        eq(HrmSalaryFixedTable::getTableId, hrmSalaryTemplateInfo.getTableId());
                        last("limit 1");
                    }});
                    if (!ObjectUtils.isEmpty(hrmSalaryFixedTable1)) {
                        hrmSalaryFixedTable.setOldBecomeAfterAmount(hrmSalaryFixedTable1.getBecomeAfterAmount());
                        hrmSalaryFixedTable.setOldBecomeFrontAmount(hrmSalaryFixedTable1.getBecomeFrontAmount());
                    }
                }
                salaryFixedTableMapper.insert(hrmSalaryFixedTable);
            }
        });
    }

    @Override
    public List<JSONObject> selectEmployeeIdListByFixed() {
        List<JSONObject> resultList = new ArrayList<>();
        List<Integer> objectStream = hrmSalaryFixedMapper.selectList(new LambdaQueryWrapper<HrmSalaryFixed>() {{
            eq(HrmSalaryFixed::getState, 1);
        }}).stream().map(h -> h.getEmployeeId()).collect(Collectors.toList());
        List<EmployeeDO> employeeDOS = employeeMapper.selectList(new LambdaQueryWrapper<EmployeeDO>() {{
            in(!ObjectUtils.isEmpty(objectStream), EmployeeDO::getId, objectStream);
            eq(ObjectUtils.isEmpty(objectStream), EmployeeDO::getId, -1);
        }});

        employeeDOS.forEach(employeeDO -> {
            JSONObject result = new JSONObject();
            result.put("id", employeeDO.getId());
            result.put("name", employeeDO.getName());
            resultList.add(result);
        });
        return resultList;
    }

    @Override
    public List<JSONObject> selectFixedUpdate() {
        List<JSONObject> resultList = new ArrayList<>();
        HrmSalaryTemplate hrmSalaryTemplate = hrmSalaryTemplateMapper.selectOne(new LambdaQueryWrapper<HrmSalaryTemplate>() {{
            last("limit 1");
        }});
        List<HrmSalaryTemplateInfo> hrmSalaryTemplateInfos = hrmSalaryTemplateInfoMapper.selectList(new LambdaQueryWrapper<HrmSalaryTemplateInfo>() {{
            eq(HrmSalaryTemplateInfo::getTemplateId, hrmSalaryTemplate.getId());
        }});
        for (HrmSalaryTemplateInfo hrmSalaryTemplateInfo : hrmSalaryTemplateInfos) {
            HrmSalaryTableConfigTenant hrmSalaryTableConfigTenant = salaryTableConfigTenantMapper.selectById(hrmSalaryTemplateInfo.getTableId());
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("id", hrmSalaryTemplateInfo.getTableId());
            jsonObject.put("tableName", hrmSalaryTableConfigTenant.getName());
            resultList.add(jsonObject);
        }
        return resultList;
    }

    @Override
    public void updateEmployeeFixedList(JSONObject param) {
//        List<Integer> employeeIdList, List<> param
//        List<Integer> employeeIdList = param.getList("employeeIdList", Integer.class);
//        Integer type = param.getInteger("type");
//        JSONArray resultList = param.getJSONArray("resultList");
//         Integer employeeStatus=-1;
//        for (Integer i : employeeIdList) {
//            if(type==1) {
//                EmployeeDO employeeDO = employeeMapper.selectById(i);
//                EmployeeSeparationRecordDO employeeSeparationRecordDO = employeeSeparationRecordMapper.selectOne(new LambdaQueryWrapper<EmployeeSeparationRecordDO>() {{
//                    eq(EmployeeSeparationRecordDO::getEmployeeId, employeeDO.getId());
//                    eq(EmployeeSeparationRecordDO::getDeleted, 0);
//                    eq(EmployeeSeparationRecordDO::getApproveResult, 2);
//                }});
//                if (employeeDO.getState() == 3 || (!ObjectUtils.isEmpty(employeeSeparationRecordDO) && employeeSeparationRecordDO.getOldStatus() == 3)) {
//                    employeeStatus = 1;
//                } else {
//                    employeeStatus = 0;
//                }
//                Integer finalEmployeeStatus = employeeStatus;
//                    HrmSalaryTemplateInfo hrmSalaryTemplateInfo =
//                    List<JSONObject> tableList = hrmSalaryFixed.getTableList();
//                    List<JSONObject> syTableList = hrmSalaryFixed.getSyTableList();
//                    Set<JSONObject> parameterBuilder = tableList.stream().filter(a -> a.getInteger("id") == hrmSalaryTemplateInfo.getTableId()).collect(Collectors.toSet());
//                    Set<JSONObject> syParameterBuilder = syTableList.stream().filter(a -> a.getInteger("id") == hrmSalaryTemplateInfo.getTableId()).collect(Collectors.toSet());
//                    if (!ObjectUtils.isEmpty(parameterBuilder)) {
//                        if (finalEmployeeStatus == 0) {
//                            if (ObjectUtils.isEmpty(hrmSalaryFixed.getSyTableList()) || ObjectUtils.isEmpty(new ArrayList<>(syParameterBuilder).get(0).getBigDecimal("becomeFrontAmount"))) {
//                                throw new ResultException("试用期内，试用期薪资不可以为空");
//                            }
//                        } else {
//                            if (ObjectUtils.isEmpty(hrmSalaryFixed.getTableList()) || ObjectUtils.isEmpty(new ArrayList<>(parameterBuilder).get(0).getBigDecimal("becomeAfterAmount"))) {
//                                throw new ResultException("转正后，正式薪资不可以为空");
//                            }
//                        }
//                        HrmSalaryFixedTable hrmSalaryFixedTable = new HrmSalaryFixedTable();
//                        if (finalEmployeeStatus == 0) {
//                            hrmSalaryFixedTable.setBecomeFrontAmount(new ArrayList<>(syParameterBuilder).get(0).getBigDecimal("becomeFrontAmount"));
//                        }
//                        hrmSalaryFixedTable.setBecomeAfterAmount(new ArrayList<>(parameterBuilder).get(0).getBigDecimal("becomeAfterAmount"));
//                        hrmSalaryFixedTable.setTableId(hrmSalaryTemplateInfo.getTableId());
//                        hrmSalaryFixedTable.setFixedId(hrmSalaryFixed.getId());
//                        hrmSalaryFixedTable.setCreateTime(DateUtil.currentDateFormat());
//                        if (!ObjectUtils.isEmpty(hrmSalaryFixed1)) {
//                            HrmSalaryFixedTable hrmSalaryFixedTable1 = salaryFixedTableMapper.selectOne(new LambdaQueryWrapper<HrmSalaryFixedTable>() {{
//                                eq(HrmSalaryFixedTable::getFixedId, hrmSalaryFixed1.getId());
//                                eq(HrmSalaryFixedTable::getTableId, hrmSalaryTemplateInfo.getTableId());
//                                last("limit 1");
//                            }});
//                            if (!ObjectUtils.isEmpty(hrmSalaryFixedTable1)) {
//                                hrmSalaryFixedTable.setOldBecomeAfterAmount(hrmSalaryFixedTable1.getBecomeAfterAmount());
//                                hrmSalaryFixedTable.setOldBecomeFrontAmount(hrmSalaryFixedTable1.getBecomeFrontAmount());
//                            }
//                        }
//                        salaryFixedTableMapper.insert(hrmSalaryFixedTable);
//                    }
//            }else {
//                BigDecimal amount=BigDecimal.ZERO;
//                hrmSalaryFixed.setEmployeeId(i);
//                Integer employeeStatus = -1;
//                if (ObjectUtils.isEmpty(hrmSalaryFixed.getTableTemplateId())) {
//                    throw new ResultException("定薪模版不能为空");
//                }
//                if (ObjectUtils.isEmpty(hrmSalaryFixed.getTableList())) {
//                    throw new ResultException("定薪项不能为空");
//                }
//                EmployeeDO employeeDO = employeeMapper.selectById(i);
//                EmployeeSeparationRecordDO employeeSeparationRecordDO = employeeSeparationRecordMapper.selectOne(new LambdaQueryWrapper<EmployeeSeparationRecordDO>() {{
//                    eq(EmployeeSeparationRecordDO::getEmployeeId, employeeDO.getId());
//                    eq(EmployeeSeparationRecordDO::getDeleted, 0);
//                    eq(EmployeeSeparationRecordDO::getApproveResult, 2);
//                }});
//                if (employeeDO.getState() == 3 || (!ObjectUtils.isEmpty(employeeSeparationRecordDO) && employeeSeparationRecordDO.getOldStatus() == 3)) {
//                    employeeStatus = 1;
//                } else {
//                    employeeStatus = 0;
//                }
//                Long l = hrmSalaryFixedMapper.selectCount(new LambdaQueryWrapper<HrmSalaryFixed>() {{
//                    eq(HrmSalaryFixed::getEmployeeId, hrmSalaryFixed.getEmployeeId());
//                    ge(HrmSalaryFixed::getDate, DateUtil.currentDateFormat());
//                }});
//                if (l > 0) {
//                    throw new ResultException("员工当前存在正在进行中的调薪任务，无法再次新增调薪任务!");
//                }
//
//
//                List<HrmSalaryTemplateInfo> hrmSalaryTemplateInfos = hrmSalaryTemplateInfoMapper.selectList(new LambdaQueryWrapper<HrmSalaryTemplateInfo>() {{
//                    eq(HrmSalaryTemplateInfo::getTemplateId, hrmSalaryFixed.getTableTemplateId());
//                }});
//                if (hrmSalaryFixed.getId() != null) {
//                    salaryFixedTableMapper.delete(new LambdaQueryWrapper<HrmSalaryFixedTable>() {{
//                        eq(HrmSalaryFixedTable::getFixedId, hrmSalaryFixed.getId());
//                    }});
//                }
//                HrmSalaryFixed hrmSalaryFixed1 = hrmSalaryFixedMapper.selectOne(new LambdaQueryWrapper<HrmSalaryFixed>() {{
//                    eq(HrmSalaryFixed::getEmployeeId, hrmSalaryFixed.getEmployeeId());
//                    orderByDesc(HrmSalaryFixed::getDate);
//                    last("limit 1");
//                }});
//                if (!ObjectUtils.isEmpty(hrmSalaryFixed1)) {
//                    hrmSalaryFixed.setOldSumSalary(hrmSalaryFixed1.getSumSalary());
//                }
//                Long l1 = hrmSalaryFixedMapper.selectCount(null);
//                if (l1 == 0) {
//                    hrmSalaryFixed.setState(0);
//                } else {
//                    hrmSalaryFixed.setState(1);
//                }
//                hrmSalaryFixed.setIsEffect(1);
//                hrmSalaryFixed.setCreateTime(DateUtil.currentDateFormat());
//                hrmSalaryFixedMapper.insert(hrmSalaryFixed);
//                Integer finalEmployeeStatus = employeeStatus;
//                for (HrmSalaryTemplateInfo hrmSalaryTemplateInfo : hrmSalaryTemplateInfos) {
//                    List<JSONObject> tableList = hrmSalaryFixed.getTableList();
//                    List<JSONObject> syTableList = hrmSalaryFixed.getSyTableList();
//                    Set<JSONObject> parameterBuilder = tableList.stream().filter(a -> a.getInteger("id") == hrmSalaryTemplateInfo.getTableId()).collect(Collectors.toSet());
//                    Set<JSONObject> syParameterBuilder = syTableList.stream().filter(a -> a.getInteger("id") == hrmSalaryTemplateInfo.getTableId()).collect(Collectors.toSet());
//                    if (!ObjectUtils.isEmpty(parameterBuilder)) {
//                        if (finalEmployeeStatus == 0) {
//                            if (ObjectUtils.isEmpty(hrmSalaryFixed.getSyTableList()) || ObjectUtils.isEmpty(new ArrayList<>(syParameterBuilder).get(0).getBigDecimal("becomeFrontAmount"))) {
//                                throw new ResultException("试用期内，试用期薪资不可以为空");
//                            }
//                        } else {
//                            if (ObjectUtils.isEmpty(hrmSalaryFixed.getTableList()) || ObjectUtils.isEmpty(new ArrayList<>(parameterBuilder).get(0).getBigDecimal("becomeAfterAmount"))) {
//                                throw new ResultException("转正后，正式薪资不可以为空");
//                            }
//                        }
//                        HrmSalaryFixedTable hrmSalaryFixedTable = new HrmSalaryFixedTable();
//                        HrmSalaryFixedTable hrmSalaryFixedTable1 = salaryFixedTableMapper.selectOne(new LambdaQueryWrapper<HrmSalaryFixedTable>() {{
//                            eq(HrmSalaryFixedTable::getFixedId, hrmSalaryFixed1.getId());
//                            eq(HrmSalaryFixedTable::getTableId, hrmSalaryTemplateInfo.getTableId());
//                            last("limit 1");
//                        }});
//                        if (!ObjectUtils.isEmpty(hrmSalaryFixedTable1)) {
//                            hrmSalaryFixedTable.setOldBecomeAfterAmount(hrmSalaryFixedTable1.getBecomeAfterAmount());
//                            hrmSalaryFixedTable.setOldBecomeFrontAmount(hrmSalaryFixedTable1.getBecomeFrontAmount());
//                        }
//                        if (finalEmployeeStatus == 0) {
//                            BigDecimal add = hrmSalaryFixedTable1.getBecomeFrontAmount().add(hrmSalaryFixedTable1.getBecomeFrontAmount().multiply(new ArrayList<>(syParameterBuilder).get(0).getBigDecimal("becomeFrontAmount").divide(new BigDecimal(100))));
//                            hrmSalaryFixedTable.setBecomeFrontAmount(add);
//                            amount=amount.add(add);
//                        }
//                        BigDecimal add = hrmSalaryFixedTable1.getBecomeAfterAmount().add(hrmSalaryFixedTable1.getBecomeAfterAmount().multiply(new ArrayList<>(parameterBuilder).get(0).getBigDecimal("becomeAfterAmount").divide(new BigDecimal(100))));
//                        amount=amount.add(add);
//                        hrmSalaryFixedTable.setBecomeAfterAmount(add);
//                        hrmSalaryFixedTable.setTableId(hrmSalaryTemplateInfo.getTableId());
//                        hrmSalaryFixedTable.setFixedId(hrmSalaryFixed.getId());
//                        hrmSalaryFixedTable.setCreateTime(DateUtil.currentDateFormat());
//                        salaryFixedTableMapper.insert(hrmSalaryFixedTable);
//                    }
//                }
//            }
//        }
    }

    @Override
    public FixedEmployeeVo getEmployeeFixedById(Integer employeeId) {
        String employeePost = "--";
        BigDecimal sumAmount = BigDecimal.ZERO;
        BigDecimal sySumAmount = BigDecimal.ZERO;
        List<JSONObject> resultList = new ArrayList<>();
        List<JSONObject> syResultList = new ArrayList<>();
        EmployeeDO employeeDO = employeeMapper.selectById(employeeId);
        HrmSalaryFixed hrmSalaryFixed = hrmSalaryFixedMapper.selectOne(new LambdaQueryWrapper<HrmSalaryFixed>() {{
            eq(HrmSalaryFixed::getEmployeeId, employeeDO.getId());
            orderByDesc(HrmSalaryFixed::getCreateTime);
            last("limit 1");
        }});
        CommonResult<PostRespDTO> post = postApi.getPost(employeeDO.getPostId());
        if (!ObjectUtils.isEmpty(post.getData())) {
            employeePost = post.getCheckedData().getName();
        }
        CommonResult<List<DictDataRespDTO>> hrmEmployeeStatus = dictDataApi.getDictDataList("hrm_employee_status");
        CommonResult<List<DictDataRespDTO>> hrmEmploymentForm = dictDataApi.getDictDataList("hrm_employment_form");
        List<DictDataRespDTO> dictDataRespDTOS = hrmEmployeeStatus.getData();
        List<DictDataRespDTO> dictForm = hrmEmploymentForm.getCheckedData();
        List<String> collect = dictDataRespDTOS.stream().filter(d -> d.getValue().equals(employeeDO.getState())).map(di -> di.getLabel()).collect(Collectors.toList());
        List<String> collect1 = dictForm.stream().filter(d -> d.getValue().equals(employeeDO.getEmploymentForm() + "")).map(di -> di.getLabel()).collect(Collectors.toList());
        List<Integer> collectId = dictForm.stream().filter(d -> d.getValue().equals(employeeDO.getEmploymentForm() + "")).map(di -> Integer.parseInt(di.getValue())).collect(Collectors.toList());
        EmployeeSeparationRecordDO employeeSeparationRecordDO = employeeSeparationRecordMapper.selectOne(new LambdaQueryWrapper<EmployeeSeparationRecordDO>() {{
            eq(EmployeeSeparationRecordDO::getEmployeeId, employeeDO.getId());
            eq(EmployeeSeparationRecordDO::getDeleted, 0);
            eq(EmployeeSeparationRecordDO::getApproveResult, 2);
        }});
        FixedEmployeeVo fixedEmployeeVo = new FixedEmployeeVo();
        fixedEmployeeVo.setEmployeeName(employeeDO.getName());
        if (!ObjectUtils.isEmpty(collect)) {
            fixedEmployeeVo.setEmployeeStatus(collect.get(0));
        } else {
            fixedEmployeeVo.setEmployeeStatus("--");
        }
        if (employeeDO.getState() == 3 || (!ObjectUtils.isEmpty(employeeSeparationRecordDO) && employeeSeparationRecordDO.getOldStatus() == 3)) {
            fixedEmployeeVo.setEmployeeState(1);
        } else {
            fixedEmployeeVo.setEmployeeState(0);
        }
        fixedEmployeeVo.setEmployeeId(employeeDO.getId().intValue());
        fixedEmployeeVo.setEmployeeNo(employeeDO.getEmployeeNo());
        fixedEmployeeVo.setEntryTime(employeeDO.getEntryTime().toString());
        if (ObjectUtils.isEmpty(employeeDO.getRegularizationTime())) {
            fixedEmployeeVo.setRegularizationTime("");
        } else {
            fixedEmployeeVo.setRegularizationTime(employeeDO.getRegularizationTime().toString());

        }
        fixedEmployeeVo.setPostName(employeePost);
        if (!ObjectUtils.isEmpty(hrmSalaryFixed)) {
            fixedEmployeeVo.setDate(hrmSalaryFixed.getDate());
//            fixedEmployeeVo.setSumSalary(hrmSalaryFixed.getSumSalary());
            fixedEmployeeVo.setCreateTime(hrmSalaryFixed.getCreateTime());
        }
        if (!ObjectUtils.isEmpty(collect1)) {
            fixedEmployeeVo.setEmployeeFrom(collect1.get(0));
        } else {
            fixedEmployeeVo.setEmployeeFrom("--");
        }

        if (!ObjectUtils.isEmpty(collectId)) {
            fixedEmployeeVo.setEmployeeFromStatus(collectId.get(0));
        } else {
            fixedEmployeeVo.setEmployeeFromStatus(1);
        }
        List<HrmSalaryFixedTable> hrmSalaryFixedTables = hrmSalaryFixedTableMapper.selectList(new LambdaQueryWrapper<HrmSalaryFixedTable>() {{
            eq(HrmSalaryFixedTable::getFixedId, hrmSalaryFixed.getId());
        }});
        for (HrmSalaryFixedTable salaryFixedTable : hrmSalaryFixedTables) {
            HrmSalaryTableConfigTenant hrmSalaryTableConfigTenant = salaryTableConfigTenantMapper.selectById(salaryFixedTable.getTableId());
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("fixedId", salaryFixedTable.getFixedId());
            jsonObject.put("id", salaryFixedTable.getTableId());
            jsonObject.put("tableName", hrmSalaryTableConfigTenant.getName());
            jsonObject.put("tableInfoId", salaryFixedTable.getId());
            jsonObject.put("becomeAfterAmount", 0);
            jsonObject.put("oldBecomeAfterAmount", salaryFixedTable.getBecomeAfterAmount());
            sumAmount = sumAmount.add(salaryFixedTable.getBecomeAfterAmount());
            resultList.add(jsonObject);
        }
        if (fixedEmployeeVo.getEmployeeState() == 0) {
            for (HrmSalaryFixedTable hrmSalaryFixedTable : hrmSalaryFixedTables) {
                HrmSalaryTableConfigTenant hrmSalaryTableConfigTenant = salaryTableConfigTenantMapper.selectById(hrmSalaryFixedTable.getTableId());
                JSONObject jsonObject = new JSONObject();
                jsonObject.put("fixedId", hrmSalaryFixedTable.getFixedId());
                jsonObject.put("id", hrmSalaryFixedTable.getTableId());
                jsonObject.put("tableName", hrmSalaryTableConfigTenant.getName());
                jsonObject.put("becomeFrontAmount", 0);
                jsonObject.put("tableInfoId", hrmSalaryFixedTable.getId());
                jsonObject.put("oldBecomeFrontAmount", hrmSalaryFixedTable.getBecomeFrontAmount());
                sySumAmount = sySumAmount.add(hrmSalaryFixedTable.getBecomeFrontAmount());
                syResultList.add(jsonObject);
            }
        }
        fixedEmployeeVo.setDate(null);
        fixedEmployeeVo.setTableTemplateId(hrmSalaryFixed.getTableTemplateId());
        fixedEmployeeVo.setTableList(resultList);
        fixedEmployeeVo.setSyTableList(syResultList);
        fixedEmployeeVo.setOldSySumSalary(sySumAmount);
        fixedEmployeeVo.setOldSumSalary(sumAmount);
        return fixedEmployeeVo;
    }


    @Override
    public List<HrmSalaryFixed> selectEmployeeFixedList(Integer employeeId) {
        List<HrmSalaryFixed> hrmSalaryFixedList = hrmSalaryFixedMapper.selectList(new LambdaQueryWrapper<HrmSalaryFixed>() {{
            eq(HrmSalaryFixed::getEmployeeId, employeeId);
            orderByDesc(HrmSalaryFixed::getDate);
        }});
        if (ObjectUtils.isEmpty(hrmSalaryFixedList)) {
            return Collections.emptyList();
        }
        hrmSalaryFixedList.forEach(hrmSalaryFixed -> {
            if (hrmSalaryFixed.getState() == 0) {
                hrmSalaryFixed.setName("定薪记录");
            } else {
                hrmSalaryFixed.setName("调薪记录");
            }
        });
        return hrmSalaryFixedList;
    }


    @Override
    public FixedEmployeeUpdateVo getEmployeeFixedInfoById(Integer id) {
        String employeePost = "--";
        List<JSONObject> syResultList = new ArrayList<>();
        HrmSalaryFixed hrmSalaryFixed = hrmSalaryFixedMapper.selectById(id);
        List<JSONObject> resultList = new ArrayList<>();
        EmployeeDO employeeDO = employeeMapper.selectById(hrmSalaryFixed.getEmployeeId());
        CommonResult<PostRespDTO> post = postApi.getPost(employeeDO.getPostId());
        if (!ObjectUtils.isEmpty(post.getData())) {
            employeePost = post.getCheckedData().getName();
        }

        CommonResult<List<DictDataRespDTO>> hrmEmployeeStatus = dictDataApi.getDictDataList("hrm_employee_status");
        List<DictDataRespDTO> dictDataRespDTOS = hrmEmployeeStatus.getData();
//        List<String> collect = dictDataRespDTOS.stream().filter(d -> d.getValue().equals(employeeDO.getState()+"")).map(di -> di.getLabel()).collect(Collectors.toList());
        FixedEmployeeUpdateVo fixedEmployeeVo = new FixedEmployeeUpdateVo();
        fixedEmployeeVo.setEmployeeName(employeeDO.getName());
        EmployeeSeparationRecordDO employeeSeparationRecordDO = employeeSeparationRecordMapper.selectOne(new LambdaQueryWrapper<EmployeeSeparationRecordDO>() {{
            eq(EmployeeSeparationRecordDO::getEmployeeId, employeeDO.getId());
            eq(EmployeeSeparationRecordDO::getDeleted, 0);
            eq(EmployeeSeparationRecordDO::getApproveResult, 2);
        }});
        if (employeeDO.getState() == 3 || (!ObjectUtils.isEmpty(employeeSeparationRecordDO) && employeeSeparationRecordDO.getOldStatus() == 3)) {
            fixedEmployeeVo.setEmployeeState(1);
        } else {
            fixedEmployeeVo.setEmployeeState(0);
        }
        fixedEmployeeVo.setRemark(hrmSalaryFixed.getRemark());
//        fixedEmployeeVo.setReason();
        fixedEmployeeVo.setId(hrmSalaryFixed.getId());
        fixedEmployeeVo.setEmployeeId(employeeDO.getId().intValue());
        fixedEmployeeVo.setEmployeeNo(employeeDO.getEmployeeNo());
        fixedEmployeeVo.setEntryTime(employeeDO.getEntryTime().toString());
        if (!ObjectUtils.isEmpty(employeeDO.getRegularizationTime())) {
            fixedEmployeeVo.setRegularizationTime(employeeDO.getRegularizationTime().toString());
        }
        fixedEmployeeVo.setPostName(employeePost);
        if (!ObjectUtils.isEmpty(hrmSalaryFixed)) {
            fixedEmployeeVo.setDate(hrmSalaryFixed.getDate());
            fixedEmployeeVo.setOldSumSalary(hrmSalaryFixed.getOldSumSalary());
            fixedEmployeeVo.setSumSalary(hrmSalaryFixed.getSumSalary());
            fixedEmployeeVo.setCreateTime(hrmSalaryFixed.getCreateTime());
            fixedEmployeeVo.setSySumSalary(hrmSalaryFixed.getSySumSalary());
            fixedEmployeeVo.setOldSySumSalary(hrmSalaryFixed.getOldSySumSalary());
        }
        List<HrmSalaryFixedTable> hrmSalaryFixedTables = hrmSalaryFixedTableMapper.selectList(new LambdaQueryWrapper<HrmSalaryFixedTable>() {{
            eq(HrmSalaryFixedTable::getFixedId, hrmSalaryFixed.getId());
        }});

        hrmSalaryFixedTables.forEach(hrmSalaryFixedTable -> {
            HrmSalaryTableConfigTenant hrmSalaryTableConfigTenant = salaryTableConfigTenantMapper.selectById(hrmSalaryFixedTable.getTableId());
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("fixedId", hrmSalaryFixedTable.getFixedId());
            jsonObject.put("id", hrmSalaryFixedTable.getTableId());
            jsonObject.put("tableInfoId", hrmSalaryFixedTable.getId());
            jsonObject.put("tableName", hrmSalaryTableConfigTenant.getName());
            jsonObject.put("becomeAfterAmount", hrmSalaryFixedTable.getBecomeAfterAmount());
            jsonObject.put("oldBecomeAfterAmount", hrmSalaryFixedTable.getOldBecomeAfterAmount());
            resultList.add(jsonObject);
        });
        if (fixedEmployeeVo.getEmployeeState() == 0) {
            hrmSalaryFixedTables.forEach(hrmSalaryFixedTable -> {
                HrmSalaryTableConfigTenant hrmSalaryTableConfigTenant = salaryTableConfigTenantMapper.selectById(hrmSalaryFixedTable.getTableId());
                JSONObject jsonObject = new JSONObject();
                jsonObject.put("fixedId", hrmSalaryFixedTable.getFixedId());
                jsonObject.put("id", hrmSalaryFixedTable.getTableId());
                jsonObject.put("tableInfoId", hrmSalaryFixedTable.getId());
                jsonObject.put("tableName", hrmSalaryTableConfigTenant.getName());
                jsonObject.put("become_after_amount", hrmSalaryFixedTable.getBecomeFrontAmount());
                jsonObject.put("old_become_after_amount", hrmSalaryFixedTable.getOldBecomeFrontAmount());
                syResultList.add(jsonObject);
            });
        }
        fixedEmployeeVo.setTableTemplateId(hrmSalaryFixed.getTableTemplateId());
        fixedEmployeeVo.setTableList(resultList);
        fixedEmployeeVo.setSyTableList(syResultList);


        return fixedEmployeeVo;
    }

    @Override
    public FixedEmployeeUpdateVo getEmployeeFixedInfoUpdateById(Integer id) {
        String employeePost = "--";
        List<JSONObject> syResultList = new ArrayList<>();
        HrmSalaryFixed hrmSalaryFixed = hrmSalaryFixedMapper.selectById(id);
        List<JSONObject> resultList = new ArrayList<>();
        EmployeeDO employeeDO = employeeMapper.selectById(hrmSalaryFixed.getEmployeeId());

        CommonResult<PostRespDTO> post = postApi.getPost(employeeDO.getPostId());
        if (!ObjectUtils.isEmpty(post.getData())) {
            employeePost = post.getCheckedData().getName();
        }
        FixedEmployeeUpdateVo fixedEmployeeVo = new FixedEmployeeUpdateVo();
        fixedEmployeeVo.setEmployeeName(employeeDO.getName());
        fixedEmployeeVo.setRemark(hrmSalaryFixed.getRemark());
        EmployeeSeparationRecordDO employeeSeparationRecordDO = employeeSeparationRecordMapper.selectOne(new LambdaQueryWrapper<EmployeeSeparationRecordDO>() {{
            eq(EmployeeSeparationRecordDO::getEmployeeId, employeeDO.getId());
            eq(EmployeeSeparationRecordDO::getDeleted, 0);
            eq(EmployeeSeparationRecordDO::getApproveResult, 2);
        }});
        if (employeeDO.getState() == 3 || (!ObjectUtils.isEmpty(employeeSeparationRecordDO) && employeeSeparationRecordDO.getOldStatus() == 3)) {
            fixedEmployeeVo.setEmployeeState(1);
        } else {
            fixedEmployeeVo.setEmployeeState(0);
        }
        fixedEmployeeVo.setId(hrmSalaryFixed.getId());
        fixedEmployeeVo.setEmployeeId(employeeDO.getId().intValue());
        fixedEmployeeVo.setEmployeeNo(employeeDO.getEmployeeNo());
        fixedEmployeeVo.setEntryTime(employeeDO.getEntryTime().toString());
        if (!ObjectUtils.isEmpty(employeeDO.getRegularizationTime())) {
            fixedEmployeeVo.setRegularizationTime(employeeDO.getRegularizationTime().toString());
        }
        fixedEmployeeVo.setReason(hrmSalaryFixed.getReason());
        fixedEmployeeVo.setPostName(employeePost);
        if (!ObjectUtils.isEmpty(hrmSalaryFixed)) {
            fixedEmployeeVo.setDate(hrmSalaryFixed.getDate());
            fixedEmployeeVo.setOldSumSalary(hrmSalaryFixed.getOldSumSalary());
            fixedEmployeeVo.setOldSySumSalary(hrmSalaryFixed.getOldSySumSalary());
            fixedEmployeeVo.setSumSalary(hrmSalaryFixed.getSumSalary());
            fixedEmployeeVo.setSySumSalary(hrmSalaryFixed.getSySumSalary());
            fixedEmployeeVo.setCreateTime(hrmSalaryFixed.getCreateTime());
        }
        List<HrmSalaryFixedTable> hrmSalaryFixedTables = hrmSalaryFixedTableMapper.selectList(new LambdaQueryWrapper<HrmSalaryFixedTable>() {{
            eq(HrmSalaryFixedTable::getFixedId, hrmSalaryFixed.getId());
        }});
        if (hrmSalaryFixed.getState() == 0) {
            hrmSalaryFixedTables.forEach(hrmSalaryFixedTable -> {
                HrmSalaryTableConfigTenant hrmSalaryTableConfigTenant = salaryTableConfigTenantMapper.selectById(hrmSalaryFixedTable.getTableId());
                JSONObject jsonObject = new JSONObject();
                jsonObject.put("fixedId", hrmSalaryFixedTable.getFixedId());
                jsonObject.put("id", hrmSalaryFixedTable.getTableId());
                jsonObject.put("tableInfoId", hrmSalaryFixedTable.getId());
                jsonObject.put("tableName", hrmSalaryTableConfigTenant.getName());
                jsonObject.put("becomeAfterAmount", hrmSalaryFixedTable.getBecomeAfterAmount());
                jsonObject.put("becomeFrontAmount", hrmSalaryFixedTable.getBecomeFrontAmount());
                resultList.add(jsonObject);
            });
            fixedEmployeeVo.setTableList(resultList);
        } else {
            hrmSalaryFixedTables.forEach(hrmSalaryFixedTable -> {
                HrmSalaryTableConfigTenant hrmSalaryTableConfigTenant = salaryTableConfigTenantMapper.selectById(hrmSalaryFixedTable.getTableId());
                JSONObject jsonObject = new JSONObject();
                jsonObject.put("fixedId", hrmSalaryFixedTable.getFixedId());
                jsonObject.put("id", hrmSalaryFixedTable.getTableId());
                jsonObject.put("tableName", hrmSalaryTableConfigTenant.getName());
                jsonObject.put("tableInfoId", hrmSalaryFixedTable.getId());
                jsonObject.put("becomeAfterAmount", hrmSalaryFixedTable.getBecomeAfterAmount());
                jsonObject.put("oldBecomeAfterAmount", hrmSalaryFixedTable.getOldBecomeAfterAmount());

                resultList.add(jsonObject);
            });
            if (fixedEmployeeVo.getEmployeeState() == 0) {
                hrmSalaryFixedTables.forEach(hrmSalaryFixedTable -> {
                    HrmSalaryTableConfigTenant hrmSalaryTableConfigTenant = salaryTableConfigTenantMapper.selectById(hrmSalaryFixedTable.getTableId());
                    JSONObject jsonObject = new JSONObject();
                    jsonObject.put("fixedId", hrmSalaryFixedTable.getFixedId());
                    jsonObject.put("id", hrmSalaryFixedTable.getTableId());
                    jsonObject.put("tableInfoId", hrmSalaryFixedTable.getId());
                    jsonObject.put("tableName", hrmSalaryTableConfigTenant.getName());
                    jsonObject.put("becomeFrontAmount", hrmSalaryFixedTable.getBecomeFrontAmount());
                    jsonObject.put("oldBecomeFrontAmount", hrmSalaryFixedTable.getOldBecomeFrontAmount());
                    syResultList.add(jsonObject);
                });
            }
        }

        fixedEmployeeVo.setTableTemplateId(hrmSalaryFixed.getTableTemplateId());
        fixedEmployeeVo.setTableList(resultList);
        fixedEmployeeVo.setSyTableList(syResultList);
        return fixedEmployeeVo;
    }


    @Override
    public void updateEmployeeFixedInfoById(HrmSalaryFixed hrmSalaryFixed) {
        Integer employeeStatus = -1;
        if (ObjectUtils.isEmpty(hrmSalaryFixed.getTableTemplateId())) {
            throw new ResultException("定薪模版不能为空");
        }
        if (ObjectUtils.isEmpty(hrmSalaryFixed.getTableList())) {
            throw new ResultException("定薪项不能为空");
        }
        EmployeeDO employeeDO = employeeMapper.selectById(hrmSalaryFixed.getEmployeeId());
        CommonResult<List<DictDataRespDTO>> hrmEmployeeStatus = dictDataApi.getDictDataList("hrm_employee_status");
        List<DictDataRespDTO> dictDataRespDTOS = hrmEmployeeStatus.getData();
        List<String> collect = dictDataRespDTOS.stream().filter(d -> d.getValue().equals(employeeDO.getState())).map(di -> di.getLabel()).collect(Collectors.toList());
        EmployeeSeparationRecordDO employeeSeparationRecordDO = employeeSeparationRecordMapper.selectOne(new LambdaQueryWrapper<EmployeeSeparationRecordDO>() {{
            eq(EmployeeSeparationRecordDO::getEmployeeId, employeeDO.getId());
            eq(EmployeeSeparationRecordDO::getDeleted, 0);
            eq(EmployeeSeparationRecordDO::getApproveResult, 2);
        }});
        FixedEmployeeVo fixedEmployeeVo = new FixedEmployeeVo();
        fixedEmployeeVo.setEmployeeName(employeeDO.getName());
        if (!ObjectUtils.isEmpty(collect)) {
            fixedEmployeeVo.setEmployeeStatus(collect.get(0));
        } else {
            fixedEmployeeVo.setEmployeeStatus("--");
        }
        if (employeeDO.getState() == 3 || (!ObjectUtils.isEmpty(employeeSeparationRecordDO) && employeeSeparationRecordDO.getOldStatus() == 3)) {
            employeeStatus = 1;
        } else {
            employeeStatus = 0;
        }
        Integer finalEmployeeStatus = employeeStatus;
        hrmSalaryFixed.setDate(null);
        hrmSalaryFixed.setCreateTime(DateUtil.currentDateFormat());
        hrmSalaryFixedMapper.updateById(hrmSalaryFixed);
        List<JSONObject> tableList = hrmSalaryFixed.getTableList();
        List<JSONObject> syTableList = hrmSalaryFixed.getSyTableList();
        tableList.forEach(table -> {
            if (!ObjectUtils.isEmpty(table)) {
                if (ObjectUtils.isEmpty(table.getBigDecimal("becomeAfterAmount"))) {
                    throw new ResultException("转正后，正式薪资不可以为空");
                }
                if (ObjectUtils.isEmpty(table.getInteger("tableInfoId"))) {
                    throw new ResultException("请刷新后重试");
                }
                Integer id = table.getInteger("tableInfoId");
                BigDecimal becomeAfterAmount = table.getBigDecimal("becomeAfterAmount");
                HrmSalaryFixedTable hrmSalaryFixedTable = hrmSalaryFixedTableMapper.selectById(id);
                hrmSalaryFixedTable.setBecomeAfterAmount(becomeAfterAmount);
                hrmSalaryFixedTableMapper.updateById(hrmSalaryFixedTable);
            }
        });
        if (finalEmployeeStatus == 0) {
            if (ObjectUtils.isEmpty(syTableList)) {
                throw new ResultException("试用期内，试用期薪资不可以为空");
            }
            syTableList.forEach(sy -> {
                if (!ObjectUtils.isEmpty(sy)) {
                    if (ObjectUtils.isEmpty(sy.getBigDecimal("becomeFrontAmount"))) {
                        throw new ResultException("转正后，正式薪资不可以为空");
                    }
                    if (ObjectUtils.isEmpty(sy.getInteger("tableInfoId"))) {
                        throw new ResultException("请刷新后重试");
                    }
                    Integer id = sy.getInteger("tableInfoId");
                    BigDecimal becomeFrontAmount = sy.getBigDecimal("becomeFrontAmount");
                    HrmSalaryFixedTable hrmSalaryFixedTable = hrmSalaryFixedTableMapper.selectById(id);
                    hrmSalaryFixedTable.setBecomeFrontAmount(becomeFrontAmount);
                    hrmSalaryFixedTableMapper.updateById(hrmSalaryFixedTable);
                }

            });

        }
    }

    @Override
    public void deletedEmployeeFixedInfoById(Integer id) {
        hrmSalaryFixedMapper.deleteById(id);
        salaryFixedTableMapper.delete(new LambdaQueryWrapper<HrmSalaryFixedTable>() {{
            eq(HrmSalaryFixedTable::getFixedId, id);
        }});
    }

    @Override
    public FixedEmployeeVo selectByEmployeeInfo(Integer employeeId) {
        String employeePost = "--";
        EmployeeDO employeeDO = employeeMapper.selectById(employeeId);
        CommonResult<PostRespDTO> post = postApi.getPost(employeeDO.getPostId());
        if (!ObjectUtils.isEmpty(post.getData())) {
            employeePost = post.getCheckedData().getName();
        }
        CommonResult<List<DictDataRespDTO>> hrmEmployeeStatus = dictDataApi.getDictDataList("hrm_employee_status");
        CommonResult<List<DictDataRespDTO>> hrmEmploymentForm = dictDataApi.getDictDataList("hrm_employment_form");
        List<DictDataRespDTO> dictDataRespDTOS = hrmEmployeeStatus.getData();
        List<DictDataRespDTO> dictForm = hrmEmploymentForm.getCheckedData();
        List<String> collect = dictDataRespDTOS.stream().filter(d -> d.getValue().equals(employeeDO.getState())).map(di -> di.getLabel()).collect(Collectors.toList());
        List<String> collect1 = dictForm.stream().filter(d -> d.getValue().equals(employeeDO.getEmploymentForm() + "")).map(di -> di.getLabel()).collect(Collectors.toList());
        List<Integer> collectId = dictForm.stream().filter(d -> d.getValue().equals(employeeDO.getEmploymentForm() + "")).map(di -> Integer.parseInt(di.getValue())).collect(Collectors.toList());
        EmployeeSeparationRecordDO employeeSeparationRecordDO = employeeSeparationRecordMapper.selectOne(new LambdaQueryWrapper<EmployeeSeparationRecordDO>() {{
            eq(EmployeeSeparationRecordDO::getEmployeeId, employeeDO.getId());
            eq(EmployeeSeparationRecordDO::getDeleted, 0);
            eq(EmployeeSeparationRecordDO::getApproveResult, 2);
        }});
        CommonResult<DeptRespDTO> dept = deptApi.getDept(employeeDO.getDeptId());
        FixedEmployeeVo fixedEmployeeVo = new FixedEmployeeVo();
        fixedEmployeeVo.setEmployeeName(employeeDO.getName());
        if (!ObjectUtils.isEmpty(collect)) {
            fixedEmployeeVo.setEmployeeStatus(collect.get(0));
        } else {
            fixedEmployeeVo.setEmployeeStatus("--");
        }
        if (employeeDO.getState() == 3 || (!ObjectUtils.isEmpty(employeeSeparationRecordDO) && employeeSeparationRecordDO.getOldStatus() == 3)) {
            fixedEmployeeVo.setEmployeeState(1);
        } else {
            fixedEmployeeVo.setEmployeeState(0);
        }
        if (!ObjectUtils.isEmpty(collectId)) {
            fixedEmployeeVo.setEmployeeFromStatus(collectId.get(0));
        } else {
            fixedEmployeeVo.setEmployeeFromStatus(1);
        }
        if (!ObjectUtils.isEmpty(collect1)) {
            fixedEmployeeVo.setEmployeeFrom(collect1.get(0));
        } else {
            fixedEmployeeVo.setEmployeeFrom("--");
        }
        if (!ObjectUtils.isEmpty(dept.getData())) {
            fixedEmployeeVo.setDepartmentName(dept.getData().getName());
        }
        fixedEmployeeVo.setPostName(employeePost);

        return fixedEmployeeVo;
    }

    @Override
    public void insertEmployeeFixedInfo(HrmSalaryFixed hrmSalaryFixed) {
        Integer employeeStatus = -1;
        if (ObjectUtils.isEmpty(hrmSalaryFixed.getTableTemplateId())) {
            throw new ResultException("定薪模版不能为空");
        }
        if (ObjectUtils.isEmpty(hrmSalaryFixed.getTableList())) {
            throw new ResultException("定薪项不能为空");
        }
        EmployeeDO employeeDO = employeeMapper.selectById(hrmSalaryFixed.getEmployeeId());

        EmployeeSeparationRecordDO employeeSeparationRecordDO = employeeSeparationRecordMapper.selectOne(new LambdaQueryWrapper<EmployeeSeparationRecordDO>() {{
            eq(EmployeeSeparationRecordDO::getEmployeeId, employeeDO.getId());
            eq(EmployeeSeparationRecordDO::getDeleted, 0);
            eq(EmployeeSeparationRecordDO::getApproveResult, 2);
        }});
        if (employeeDO.getState() == 3 || (!ObjectUtils.isEmpty(employeeSeparationRecordDO) && employeeSeparationRecordDO.getOldStatus() == 3)) {
            employeeStatus = 0;
        } else {
            employeeStatus = 1;
        }
        Long l = hrmSalaryFixedMapper.selectCount(new LambdaQueryWrapper<HrmSalaryFixed>() {{
            eq(HrmSalaryFixed::getEmployeeId, hrmSalaryFixed.getEmployeeId());
            ge(HrmSalaryFixed::getDate, DateUtil.currentDateFormat());
        }});
        if (l > 0) {
            throw new ResultException("员工当前存在正在进行中的调薪任务，无法再次新增调薪任务!");
        }
        List<HrmSalaryTemplateInfo> hrmSalaryTemplateInfos = hrmSalaryTemplateInfoMapper.selectList(new LambdaQueryWrapper<HrmSalaryTemplateInfo>() {{
            eq(HrmSalaryTemplateInfo::getTemplateId, hrmSalaryFixed.getTableTemplateId());
        }});
        if (hrmSalaryFixed.getId() != null) {
            salaryFixedTableMapper.delete(new LambdaQueryWrapper<HrmSalaryFixedTable>() {{
                eq(HrmSalaryFixedTable::getFixedId, hrmSalaryFixed.getId());
            }});
        }
        HrmSalaryFixed hrmSalaryFixed1 = hrmSalaryFixedMapper.selectOne(new LambdaQueryWrapper<HrmSalaryFixed>() {{
            eq(HrmSalaryFixed::getEmployeeId, hrmSalaryFixed.getEmployeeId());
            orderByDesc(HrmSalaryFixed::getDate);
            last("limit 1");
        }});
        if (!ObjectUtils.isEmpty(hrmSalaryFixed1)) {
            hrmSalaryFixed.setOldSumSalary(hrmSalaryFixed1.getSumSalary());
        }
        Long l1 = hrmSalaryFixedMapper.selectCount(new LambdaQueryWrapper<HrmSalaryFixed>() {{
            eq(HrmSalaryFixed::getEmployeeId, hrmSalaryFixed.getEmployeeId());
        }});
        if (l1 == 0) {
            hrmSalaryFixed.setState(0);
        } else {
            hrmSalaryFixed.setState(1);
        }
        hrmSalaryFixed.setDate(DateUtil.currentDateYearFormat());
        hrmSalaryFixed.setIsEffect(1);
        hrmSalaryFixed.setCreateTime(DateUtil.currentDateFormat());
        hrmSalaryFixedMapper.insert(hrmSalaryFixed);
        Integer finalEmployeeStatus = employeeStatus;
        hrmSalaryTemplateInfos.forEach(hrmSalaryTemplateInfo -> {
            List<JSONObject> tableList = hrmSalaryFixed.getTableList();
            System.err.println();
            Set<JSONObject> parameterBuilder = tableList.stream().filter(a -> a.getInteger("id").intValue() == hrmSalaryTemplateInfo.getTableId()).collect(Collectors.toSet());
            if (!ObjectUtils.isEmpty(parameterBuilder)) {
                if (finalEmployeeStatus == 0) {
                    if (ObjectUtils.isEmpty(new ArrayList<>(parameterBuilder).get(0).getBigDecimal("becomeFrontAmount"))) {
                        throw new ResultException("试用期内，试用期薪资不可以为空");
                    }
                } else {
                    if (ObjectUtils.isEmpty(new ArrayList<>(parameterBuilder).get(0).getBigDecimal("becomeAfterAmount"))) {
                        throw new ResultException("转正后，正式薪资不可以为空");
                    }
                }
                HrmSalaryFixedTable hrmSalaryFixedTable = new HrmSalaryFixedTable();
                hrmSalaryFixedTable.setBecomeAfterAmount(new ArrayList<>(parameterBuilder).get(0).getBigDecimal("becomeAfterAmount"));
                hrmSalaryFixedTable.setBecomeFrontAmount(new ArrayList<>(parameterBuilder).get(0).getBigDecimal("becomeFrontAmount"));
                hrmSalaryFixedTable.setTableId(hrmSalaryTemplateInfo.getTableId());
                hrmSalaryFixedTable.setFixedId(hrmSalaryFixed.getId());
                hrmSalaryFixedTable.setCreateTime(DateUtil.currentDateFormat());
                if (!ObjectUtils.isEmpty(hrmSalaryFixed1)) {
                    HrmSalaryFixedTable hrmSalaryFixedTable1 = salaryFixedTableMapper.selectOne(new LambdaQueryWrapper<HrmSalaryFixedTable>() {{
                        eq(HrmSalaryFixedTable::getFixedId, hrmSalaryFixed1.getId());
                        eq(HrmSalaryFixedTable::getTableId, hrmSalaryTemplateInfo.getTableId());
                        last("limit 1");
                    }});
                    if (!ObjectUtils.isEmpty(hrmSalaryFixedTable1)) {
                        hrmSalaryFixedTable.setOldBecomeAfterAmount(hrmSalaryFixedTable1.getBecomeAfterAmount());
                        hrmSalaryFixedTable.setOldBecomeFrontAmount(hrmSalaryFixedTable1.getBecomeFrontAmount());
                    }
                }
                salaryFixedTableMapper.insert(hrmSalaryFixedTable);
            }
        });
    }

    @Override
    public void updateEmployeeFixedInfo(HrmSalaryFixed hrmSalaryFixed) {
        Integer employeeStatus = -1;
        if (ObjectUtils.isEmpty(hrmSalaryFixed.getTableTemplateId())) {
            throw new ResultException("定薪模版不能为空");
        }
        if (ObjectUtils.isEmpty(hrmSalaryFixed.getTableList())) {
            throw new ResultException("定薪项不能为空");
        }
        hrmSalaryFixed.setDate(null);
        hrmSalaryFixed.setCreateTime(DateUtil.currentDateFormat());
        hrmSalaryFixedMapper.updateById(hrmSalaryFixed);
        List<JSONObject> tableList = hrmSalaryFixed.getTableList();
        tableList.forEach(table -> {
            if (!ObjectUtils.isEmpty(table)) {
                if (employeeStatus == 0) {
                    if (ObjectUtils.isEmpty(table.getBigDecimal("becomeFrontAmount"))) {
                        throw new ResultException("试用期内，试用期薪资不可以为空");
                    }
                } else {
                    if (ObjectUtils.isEmpty(table.getBigDecimal("becomeAfterAmount"))) {
                        throw new ResultException("转正后，正式薪资不可以为空");
                    }
                }

                if (ObjectUtils.isEmpty(table.getInteger("tableInfoId"))) {
                    throw new ResultException("请刷新后重试");
                }
                Integer id = table.getInteger("tableInfoId");
                BigDecimal becomeAfterAmount = table.getBigDecimal("becomeAfterAmount");
                BigDecimal becomeFrontAmount = table.getBigDecimal("becomeFrontAmount");
                HrmSalaryFixedTable hrmSalaryFixedTable = hrmSalaryFixedTableMapper.selectById(id);
                hrmSalaryFixedTable.setBecomeFrontAmount(becomeFrontAmount);
                hrmSalaryFixedTable.setBecomeAfterAmount(becomeAfterAmount);
                hrmSalaryFixedTableMapper.updateById(hrmSalaryFixedTable);
            }
        });
    }
}







